﻿<template>
  <l-layout class="l-tab-page" v-if="!lr_loadPage">
    <l-panel>
      <template #toolLeft>
        <l-query2
          :items="queryItems"
          :formData="queryData"
          :loading="tableLoading"
          @search="handleSearch"
        >
          <template #f_Code>
            <el-input v-model="queryData.f_Code" :placeholder="$t('please enter')" />
          </template>
          <template #f_Name>
            <el-input v-model="queryData.f_Name" :placeholder="$t('please enter')" />
          </template>
          <template #f_EnabledMark>
            <el-switch
              :activeValue="1"
              :inactiveValue="0"
              v-model="queryData.f_EnabledMark"
            />
          </template>
        </l-query2>
      </template>
      <template #toolRight>
        <l-tool-btns
          :hasTableSetting="true"
          @setting="lr_handleSettingTable"
          @click="handleAdd()"
        >
          <l-excel-btns></l-excel-btns>
        </l-tool-btns>
      </template>
      <l-table
        ref="mainTable"
        :loading="tableLoading"
        :columns="lr_getPageColumns(columns)"
        :dataSource="tableData"
        :isPage="true"
        :pageTotal="tableTotal"
        :tablePage.sync="tableCurrentPage"
        @loadPageData="turnTablePage"
      >
        <template v-slot:f_EnabledMark="scope">
          <el-switch
            :active-value="1"
            :inactive-value="0"
            v-model="scope.row.f_EnabledMark"
            @change="handleEnableChange(scope.row)"
          >
          </el-switch>
        </template>
        <l-table-btns
          :btns="tableBtns"
          @click="lr_handleTableBtnClick"
        ></l-table-btns>
      </l-table>
    </l-panel>

    <!--表单-->
    <l-dialog
      :title="$t(formTitle)"
      :visible.sync="formVisible"
      :height="400"
      :width="600"
      @ok="handleSave"
      @close="handleCloseForm"
      @opened="handleOpenedForm"
    >
      <my-form ref="form"></my-form>
    </l-dialog>

    <l-fullscreen-dialog
      :title="`${$t('租户组授权')}`"
      :visible.sync="authVisible"
      :showOk="false"
      :closeEvent="handleClosed"
      @opened="handleAuthOpened"
    >
      <auth-index ref="authIndex"></auth-index>
    </l-fullscreen-dialog>

    <!--根据名称批量Delete-->
    <l-dialog
      :title="$t('批量删租户')"
      :visible.sync="deleteFormVisible"
      :height="110"
      :width="400"
      @ok="handleDeleteTenants"
      @close="handleCloseDeleteForm"
    >
      <delete-form ref="deleteform" />
    </l-dialog>

    <!--设置数据同步模版-->
    <l-dialog
      :title="$t('数据同步模版')"
      :visible.sync="dataSchemaVisible"
      :height="600"
      :width="1000"
      @opened="dataSchemaIndexOpened"
      :hasBtns="false"
    >
      <dataschema-index :objectId="objectId" ref="dataschema" />
    </l-dialog>
  </l-layout>
</template>

<script>
import MyForm from "./form";
import AuthIndex from "../auth/index.vue";
import DeleteForm from "./deleteForm.vue";
import DataschemaIndex from "../dataSchema/index.vue";

const api = window.$api.sass.tenantGroup;

export default {
  components: {
    MyForm,
    AuthIndex,
    DeleteForm,
    DataschemaIndex,
  },
  data() {
    return {
      lr_isPage: true,
      moduleCode: "sass_tenantGroup",

      // query参数
      queryData: {
        f_Code: "",
        f_Name: "",
        f_EnabledMark: 1,
      },

      queryItems: [
        { label: this.$t("编码"), prop: "f_Code" },
        { label: this.$t("名称"), prop: "f_Name" },
        { label: this.$t("有效"), prop: "f_EnabledMark" },
      ],

      // 表格参数
      tableLoading: false,
      tableData: [],
      tableTotal: 0,
      tableCurrentPage: 1,
      tablePageSize: 50,
      columns: [
        {
          label: "名称",
          prop: "f_Name",
          minWidth: 120,
          align: "left",
          dataType: "input",
        },
        { label: "有效", prop: "f_EnabledMark", width: 80, align: "center" },
      ],
      tableBtns: [
        { prop: "Edit", label: this.$t("Edit"),width:40 },
        { prop: "Auth", label: this.$t("授权"),width:40 },
        { prop: "Update", label: this.$t("同步"),width:40 },
        { prop: "Dataschema", label: this.$t("数据同步配置"), width: 80 },
        { prop: "Delete", label: this.$t("Delete"),width:40 },
        { prop: "OpenDeleteForm", label: this.$t("批量Delete租户"), width: 80 },
      ],

      // 表单参数
      formVisible: false,
      formTitle: "",
      formEditRow: null,
      formEdit: false,

      authVisible: false,

      deleteFormVisible: false,

      dataSchemaVisible: false,
    };
  },
  computed: {
    objectId() {
      if (this.formEditRow) {
        return this.formEditRow.f_Id;
      }
      return "";
    },
  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
      this.loadTableData();
    },

    handleSearch() {
      this.loadTableData();
    },
    turnTablePage({ rows }) {
      this.tablePageSize = rows;
      this.loadTableData(true);
    },
    async loadTableData(isNotFirst) {
      if (!isNotFirst) {
        this.tableCurrentPage = 1;
      }
      this.tableLoading = true;
      let queryData = this.$deepClone(this.queryData);

      queryData.rows = this.tablePageSize;
      queryData.page = this.tableCurrentPage;
      queryData.sidx = "F_Id";
      let data = await this.$awaitWraper(api.getPage(queryData));
      if (data != null) {
        this.tableData = data.rows;
        this.tableTotal = data.records;
      } else {
        this.tableData = [];
      }
      this.tableLoading = false;
    },

    handleAdd() {
      this.formEdit = false;
      this.handleShowForm("Add");
    },
    handleEdit($index, row) {
      this.formEdit = true;
      this.formEditRow = row;
      this.handleShowForm("Edit");
    },
    handleDelete($index, row) {
      this.$confirm(
        this.$t("This operation will permanently delete the data. Do you want to continue?"),
        this.$t("Tips"),
        {
          confirmButtonText: this.$t("confirm"),
          cancelButtonText: this.$t("cancel"),
          type: "warning",
        }
      )
        .then(async () => {
          this.tableData.splice($index, 1);
          this.tableTotal--;
          await api.remove(row.f_Id);
          this.$message({
            type: "success",
            message: this.$t("Delete  success"),
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: this.$t("Delete has been canceled!"),
          });
        });
    },

    handleDataschema($index, row) {
      this.formEditRow = row;
      this.dataSchemaVisible = true;
    },

    async handleSave(showLoading, hideLoading) {
      showLoading("saving data...");
      if (await this.$refs.form.validateForm()) {
        const res = await this.$refs.form.saveForm({
          keyValue: this.formEdit ? this.formEditRow.f_Id : "",
          isEdit: this.formEdit,
        });
        if (res) {
          const msg = this.formEdit ? this.$t("更新成功!") : this.$t("Add成功!");
          this.$message({
            type: "success",
            message: this.$t(msg),
          });
          this.loadTableData();
          this.formVisible = false;
        }
      }
      hideLoading();
    },

    handleShowForm(text) {
      this.formTitle = text;
      this.formVisible = true;
    },
    async handleOpenedForm(showLoading, hideLoading) {
      if (this.formEdit) {
        showLoading("Loading data...");
        await this.$refs.form.loadFormData({ keyValue: this.formEditRow.f_Id });
        hideLoading();
      }
    },
    handleCloseForm() {
      this.$refs.form.resetForm();
    },

    // 授权
    handleAuth($index, row) {
      this.formEditRow = row;
      this.authVisible = true;
    },
    async handleClosed() {
      await this.$refs.authIndex.close();
      return true;
    },
    async handleAuthOpened(showLoading, hideLoading) {
      showLoading("加载权限数据");
      await this.$refs.authIndex.init(this.formEditRow.f_Id);
      hideLoading();
    },
    async handleEnableChange(row) {
      const res = await this.$awaitWraper(api.update(row.f_Id, row));
      if (res) {
        this.$message({
          type: "success",
          message: this.$t("更新成功!"),
        });
      }
    },

    handleUpdate($index, row) {
      this.$confirm(this.$t("是否同步数据?"), this.$t("Tips"), {
        confirmButtonText: this.$t("confirm"),
        cancelButtonText: this.$t("cancel"),
        type: "warning",
      })
        .then(async () => {
          await api.updateTask(row.f_Id);
          this.$message({
            type: "success",
            message: this.$t("提交成功!"),
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: this.$t("已cancel同步!"),
          });
        });
    },

    handleOpenDeleteForm($index, row) {
      this.formEditRow = row;
      this.deleteFormVisible = true;
    },
    handleDeleteTenants() {
      let loading = null;
      this.$confirm(this.$t("是否批量Delete?"), this.$t("Tips"), {
        confirmButtonText: this.$t("confirm"),
        cancelButtonText: this.$t("cancel"),
        type: "warning",
      })
        .then(async () => {
          loading = this.$loading({
            lock: true,
            text: this.$t("File 导出中"),
            spinner: "el-icon-loading",
            background: "rgba(0, 0, 0, 0.7)",
          });
          await this.$refs.deleteform.delete(this.formEditRow.f_Id);
          loading.close();
          this.deleteFormVisible = false;
          this.$message({
            type: "success",
            message: this.$t("Delete  success"),
          });
        })
        .catch(() => {
          if (loading) {
            loading.close();
          }
          this.$message({
            type: "info",
            message: this.$t("Delete has been canceled!"),
          });
        });
    },
    handleCloseDeleteForm() {
      this.$refs.deleteform.resetForm();
    },

    async dataSchemaIndexOpened(showLoading, hideLoading) {
      showLoading("加载配置数据");
      await this.$refs.dataschema.handleSearch();
      hideLoading();
    },
  },
};
</script>
